<?php
//error_reporting(E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR);
require('./roots.php');
require($root_path.'include/inc_environment_global.php');

define('LANG_FILE','interconsulta.php');
define('NO_2LEVEL_CHK',1);
require_once($root_path.'include/inc_front_chain_lang.php');

$login=$_SESSION['sess_login_userid'];

$thisfile=basename(__FILE__);
$displayfile='interconsulta.php'.URL_APPEND;
$action='interconsulta.php'.URL_APPEND;

require_once($root_path.'include/care_api_classes/class_core.php');
require_once($root_path.'include/care_api_classes/class_interconsulta.php');
require_once('./include/mail.php');
$core=new Core;
$interconsulta = new Interconsulta;

//  echo "<pre>";
//  var_dump($_SESSION);
//  echo "</pre>";

if ($update) { //Actualizacion de interconsulta
        $temp = $interconsulta->updateDataFromInternalArray($id);

	$files = explode(' ', $listaArchivos);

        if ($res)
        {
            $resp = "_resp";
        }

	foreach($files as $fileInput){
		$interconsulta->guardarAdjunto(trim($fileInput), $resp);
	}

        $msj = $LDUsers.$login.$LDActulizarInter.$id;

	if($res){
            $msj = $LDuser.$login.$LDResponderInte.$id;
		?>
		<script  language="javascript">
			window.close();
		</script>
		<?php
	}

        $interconsulta->insertarBitacora($msj);

        if ($temp)
        {
            $msj = $LDActualizarCorrec;
        }
        else
        {
            $msj = $LDActualizarNoCorrec;
        }

	header("Location:interconsulta.php".URL_APPEND."&lang=$lang&msj=$msj");
	exit;

} else  { //Insercion de interconsulta
        $temp = $interconsulta->insertDataFromInternalArray();
	$files = explode(' ', $listaArchivos);

        if ($respuesta == 1)
        {
            $resp = "_resp";
        }

	foreach($files as $fileInput ){
		$interconsulta->guardarAdjunto(trim($fileInput), $resp);
	}

        $msj = $LDUsers.$login.$LDCrearInter;
        $interconsulta->insertarBitacora($msj);

	//Notificar al grupo de triaje
	sendMail();

        if ($temp)
        {
            $msj =$LDCrearCorrec;
        }
        else
        {
            $msj =$LDCrearNoCorrec;
        }

	header("Location:interconsulta.php".URL_APPEND."&lang=$lang&msj=$msj");
	exit;

}

function sendMail() {
	$inter = new Interconsulta;
        $emails = $inter->getAllMailTriaje();

	if($emails!=NULL){
		foreach($emails as $email){
			if($email != '') enviar_mail($email, $LDNewInterSubject, $LDNewInterBody);

		}
	}
}


?>
